package com.example.AiLangLearn.embedding;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.AiLangLearn.R;
import com.iflytek.sparkchain.core.Embedding;
import com.iflytek.sparkchain.core.EmbeddingOutput;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.util.ArrayList;

public class EmbeddingActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "AEE_Embedding";
    private Button bt_EmbeddingP,bt_EmbeddingQ;

    private TextView tv_notification;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_embedding);
        initView();
    }

    private void initView(){
        bt_EmbeddingP = findViewById(R.id.embedding_startEmbeddingP);
        bt_EmbeddingQ = findViewById(R.id.embedding_startEmbeddingQ);
        bt_EmbeddingP.setOnClickListener(this);
        bt_EmbeddingQ.setOnClickListener(this);
        tv_notification = findViewById(R.id.embedding_notification);
    }

    private void showInfo(String text){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_notification.setText(text);
            }
        });
    }

    private void startEmbedding(String input,String domain,String filePath){
        EmbeddingOutput output = Embedding.getInst().embedding(input,domain);
        int ret = output.getErrCode();
        Log.d(TAG,"raw:"+output.getRaw());
        Log.d(TAG,"ret:"+ret);
        Log.d(TAG,"msg:"+output.getErrMsg());
        if(ret == 0){
            ArrayList<Float> af = output.getResultArray();
            try(BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))){
                for(float value : af){
                    BigDecimal bm = new BigDecimal(value);
                    writer.write(bm.toString());
                    writer.newLine();
                }
                showInfo("写入文件成功。路径为："+filePath);
            }catch (Exception e){
                e.printStackTrace();
                showInfo("写入文件失败。请检查路径是否有读写权限:" + filePath);
            }
        }else{
            showInfo("Embedding转换失败，错误码：" + ret);
        }
    }



    /***********
     * 文档向量化建模
     * ***************/
    private void startEmbeddingP(){
        String input = "这段话的内容变成向量化是什么样的？";
        String domain = "para";
        String filePath = "/sdcard/iflytek/embedding_androidP.txt";
        startEmbedding(input,domain,filePath);
    }
    /***********
    * 问题向量化建模
    * ***************/
    private void startEmbeddingQ(){
        String input = "这段话的内容变成向量化是什么样的？";
        String domain = "query";
        String filePath = "/sdcard/iflytek/embedding_androidQ.txt";
        startEmbedding(input,domain,filePath);
    }


    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.embedding_startEmbeddingP:
                startEmbeddingP();
                break;
            case R.id.embedding_startEmbeddingQ:
                startEmbeddingQ();
                break;
        }
    }
}
